package com.java.Servlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 浏览器中通过`http://localhost:8090/java-web/servlet`访问服务端并响应
 */

// 通过xml配置的方案已被注解替代，若写注解则无需在xml中配置
@WebServlet("/servlet")
public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
        // 利用输出流将数据写出给客户端
        res.setContentType("text/html;charset=utf-8"); // 防止后端响应数据乱码
        PrintWriter out = res.getWriter(); // 获取输出流写数据
        out.println("<html>");
        out.println("<head><meta charset='utf-8'><title>第一个</title></head>"); // 此处可防止浏览器渲染响应数据时乱码
        out.println("<body>");
        out.println("<p style='color:red'>你好</p>");
        out.println("</body>");
        out.println("</html>");
    }
}